﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MartinsEssentials.MVVM;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.Diagnostics;
namespace MALRECrewrite.Models
{
    public class SortedRecommendation : Recommendation, INotifyPropertyChanged
    {
        private int _CountSum;
        public int CountSum
        {
            get
            {
                return _CountSum;
            }
            set
            {
                if (value != _CountSum)
                {
                    _CountSum = value;
                    OnPropertyChanged("CountSum");
                }
            }
        }

        private int _SourceSum = 1;
        public int  SourceSum
        {
            get
            {
                return _SourceSum;
            }
            set
            {
                if (value != _SourceSum)
                {
                    _SourceSum = value;
                    OnPropertyChanged("SourceSum");
                }
            }
        }

        private string _Image;
        public override string Image
        {
            get
            {
                return _Image;
            }
            set
            {
                if (value != _Image)
                {
                    _Image = value;
                    OnPropertyChanged("Image");
                }
            }
        }

        private List<string> _Genres;
        public override List<string> Genres
        {
            get
            {
                return _Genres;
            }
            set
            {
                if (value != _Genres)
                {
                    _Genres = value;
                    OnPropertyChanged("Genres");
                }
            }
        }

        private bool _ShowAllInfo = false;
        public bool ShowAllInfo
        {
            get
            {
                return _ShowAllInfo;
            }
            set
            {
                if (value != _ShowAllInfo)
                {
                    _ShowAllInfo = value;
                    OnPropertyChanged("ShowAllInfo");
                }
            }
        }

        public SortedRecommendation()
        {
        }

        private ObservableCollection<string> _Sources;
        public ObservableCollection<string> Sources
        {
            get
            {
                return _Sources;
            }
            set
            {
                if (value != _Sources)
                {
                    _Sources = value;
                    OnPropertyChanged("Sources");
                }
            }
        }

        public SortedRecommendation(Recommendation recommendation)
        {
            CountSum = recommendation.Count;
            Title = recommendation.Title;
            Aid = recommendation.Aid;
            Uri = recommendation.Uri;
            MemberScore = recommendation.MemberScore;
            Rank = recommendation.Rank;
            Image = recommendation.Image;
            IsEnglish = recommendation.IsEnglish;
            IsGerman = recommendation.IsGerman;
            LangUnknown = recommendation.LangUnknown;
            IsJapanese = recommendation.IsJapanese;
        }

        private bool _LangUnknown;
        public bool LangUnknown
        {
            get
            {
                return _LangUnknown;
            }
            set
            {
                if (value != _LangUnknown)
                {
                    _LangUnknown = value;
                    OnPropertyChanged("LangUnknown");

                    var recs = App.DataModel.Animes.Where(x => x.Recommendations.Count(y => y.Aid == Aid) > 0).ToList();
                    foreach (Anime a in recs)
                    {
                        a.Recommendations.Where(x => x.Aid == Aid).ToList().ForEach(x => x.LangUnknown = _LangUnknown);
                    }
                }
            }
        }

        private bool _IsGerman;
        public bool IsGerman
        {
            get
            {
                return _IsGerman;
            }
            set
            {
                if (value != _IsGerman)
                {
                    _IsGerman = value;
                    OnPropertyChanged("IsGerman");

                    var recs = App.DataModel.Animes.Where(x => x.Recommendations.Count(y => y.Aid == Aid) > 0).ToList();
                    foreach (Anime a in recs)
                    {
                        a.Recommendations.Where(x => x.Aid == Aid).ToList().ForEach(x => x.IsGerman = _IsGerman);
                    }
                }
            }
        }

        private bool _IsEnglish;
        public bool IsEnglish
        {
            get
            {
                return _IsEnglish;
            }
            set
            {
                if (value != _IsEnglish)
                {
                    _IsEnglish = value;
                    OnPropertyChanged("IsEnglish");

                    var recs = App.DataModel.Animes.Where(x => x.Recommendations.Count(y => y.Aid == Aid) > 0).ToList();
                    foreach (Anime a in recs)
                    {
                        a.Recommendations.Where(x => x.Aid == Aid).ToList().ForEach(x => x.IsEnglish = _IsEnglish);
                    }
                }
            }
        }

        private bool _IsJapanese;
        public bool IsJapanese
        {
            get
            {
                return _IsJapanese;
            }
            set
            {
                if (value != _IsJapanese)
                {
                    _IsJapanese = value;
                    OnPropertyChanged("IsJapanese");

                    var recs = App.DataModel.Animes.Where(x => x.Recommendations.Count(y => y.Aid == Aid) > 0).ToList();
                    foreach (Anime a in recs)
                    {
                        a.Recommendations.Where(x => x.Aid == Aid).ToList().ForEach(x => x.IsJapanese = _IsJapanese);
                    }
                }
            }
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, args);
        }

        #endregion Implementation of INotifyPropertyChanged        
    }
}
